<template>
  <div class="login_container bg_move">
    <!-- 登录面板 -->
    <div class="login_div login_show">
      <!-- 头像 -->
      <div class="avatar avatar_show" />
      <!-- 表单 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login_form" label-width="0px">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-3702mima" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item class="button_click button_show">
          <el-button type="primary" @click="checkLoginForm">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单的数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的规则验证对象
      loginRules: {
        // 验证用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        // 验证密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单数据
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 检查表单数据
    checkLoginForm() {
      this.$refs.loginFormRef.validate(async valid => {
        // 第一个形参是 结果值  true 或者 false
        // 返回false 直接return
        if (!valid) return false
        // 解构赋值 得到 data 数据，并给它起了别名 res
        const { data: res } = await this.$http.post('login', this.loginForm)
        // 判断是否登录成功
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        // 把登录的成功后的 token信息保存到 sessionStroge 中
        // localStroge 是持久信息
        // sessionStroge 是会话信息
        window.sessionStorage.setItem('token', res.data.token)
        // 通过路由跳转到home  去router下面的index 建立home路由
        this.$router.push('/home')
      })
    }
  }

}
</script>

<style lang="less" scoped>
  .login_container{
    position: relative;
    height: 100%;
    background: linear-gradient(182deg,rgba(51,153,255,1),rgba(0,51,153,1),rgba(51,153,255,1),rgba(51,153,255,1));
  }
  // 移动背景
  .bg_move {
    background-size: 1000% 1000%;
    // background-repeat: no-repeat;
    animation: bg_move 15s ease infinite;
  }
  @keyframes bg_move {
    0%,100%{
      background-position: -200% -200%;
    }25%{
      background-position: 200% -200%;
    }
    50%{
      background-position: 200% 200%;
    }
    75%{
      background-position: -200% 200%;
    }
  }
  // 登录页面的出现
  .login_show {
    animation: login_show cubic-bezier(0, 0.51, 0.68, 1.2) 1s forwards;
  }
  @keyframes login_show {
    0%{
      top: -100%;
      transform: scale(0.5) translate(-50%,-50%);
    }
    100%{
      top: 50%;
      transform: scale(1) translate(-50%,-50%);
    }
  }

  // 登录盒子
  .login_div{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 650px;
    height: 400px;
    border-radius: 8px;
    background-color: rgba(255,255,255,1);
    // background-image: url(/img/bg.1b57c02a.jpg);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    // filter: blur(1px);
    box-shadow: 0 2px 3px 3px rgb(51 153 255);
  }
    // 头像出现
  .avatar_show {
    animation: avatar_show cubic-bezier(0, 0.51, 0.68, 1.2) 1s 1s forwards;
  }
  @keyframes avatar_show {
    0%{
      opacity: 0;
      transform: translate(-50%, 50%);
    }
    100%{
      opacity: 1;
      transform: translate(-50%, -50%);
    }
  }
  // 头像
  .avatar{
    opacity: 0;
    position: absolute;
    transform: translate(-50%, -50%);
    left: 50%;
    border-radius: 50%;
    width: 180px;
    height: 180px;
    border: 13px solid #dde6f6;
    background: url("../assets/logo.png") no-repeat center;
    background-size: 100% 100%;
    background-color: #ffffff;
    box-shadow: 0 2px 3px 3px #c3e1ff;
  }
  // 登录表单
  .login_form{
   position: absolute;
    bottom: 0;
    width: 100%;
    height: 100%;
    padding: 147px 45px;
    box-sizing: border-box;
  }
   // 按钮出现
  .button_show {
    animation: button_show cubic-bezier(0, 0.51, 0.68, 1.2) 1s 2s forwards;
  }
  @keyframes button_show {
    0%{
      opacity: 0;
      transform: translate(-50%, 50%);
    }
    50%{
      transform: translate(-70%,0);
    }
    60%{
      transform: translate(-20%,0);
    }
    70%{
      transform: translate(-60%,0);
    }80%{
      transform: translate(-40%,0);
    }90%{
      transform: translate(-54%,0);
    }
    100%{
      opacity: 1;
      transform: translate(-50%, 0%);
    }
  }
  // 两个按钮
  .button_click{
    position: absolute;
    bottom: 10%;
    left: 50%;
    transform: translate(-50%,0);
    opacity: 0;
  }
</style>
